﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication.Models;
using System.Net.Mime;

namespace MvcApplication.Controllers
{
    public class NhanSuController : Controller
    {
        //
        // GET: /NhanSu/

        Winapp111020DataContext dataContext = new Winapp111020DataContext();
        /// <summary>
        /// Display list NhanSu
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var nhansus = from n in dataContext.NhanSuModels
                         select n;
            return View(nhansus);
        }

        //[HttpPost]
        //public FileContentResult ByteToPhoto(byte[] photo)
        //{
        //    return File(photo, "image/jpeg");
        //}
        /// <summary>
        /// Display image of NhanSu
        /// </summary>
        /// <param name="id">Id of NhanSu</param>
        /// <returns></returns>
        public FileContentResult IdToPhoto(int id)
        {
            return File(dataContext.NhanSuModels.First(n => n.id_nhan_su == id).hinh_anh, MediaTypeNames.Image.Jpeg);
        }

        /// <summary>
        /// Create new NhanSu
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Display details of NhanSu
        /// </summary>
        /// <param name="id">Id of NhanSu</param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            var nhansu = from n in dataContext.NhanSuModels
                         where n.id_nhan_su == id
                         select n;
            return View(nhansu.SingleOrDefault());
        }

        /// <summary>
        /// Display details of NhanSu to edit
        /// </summary>
        /// <param name="id">Id of NhanSu</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var nhansu = from n in dataContext.NhanSuModels
                         where n.id_nhan_su == id
                         select n;
            return View(nhansu.SingleOrDefault());
        }

        /// <summary>
        /// Display details of NhanSu to preview before edit
        /// </summary>
        /// <param name="id">Id of NhanSu</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            var nhansu = from n in dataContext.NhanSuModels
                         where n.id_nhan_su == id
                         select n;
            return View(nhansu.SingleOrDefault());
        }
        [HttpPost]
        public ActionResult Delete(FormCollection fc, int id)
        {
            var nhansu = from n in dataContext.NhanSuModels
                         where n.id_nhan_su == id
                         select n;
            nhansu.SingleOrDefault().deleted = true;
            dataContext.SubmitChanges();
            return RedirectToAction("index");
        }
    }
}
